/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package study.alex.db;

import java.sql.*;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author RERIR
 */
public class DemoDb {
    
    private static Connection conn;
    private static Statement st;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
/*        printJdbcDrivers();
        loadDriver("org.apache.derby");
        printJdbcDrivers();
        loadDriver("org.apache.derby.jdbc.ClientDriver");
        connect("jdbc:derby://localhost:1527/test");
//        connect("jdbc:derby://localhost:1527/test");
        System.out.println(conn);
//        executesql("INSERT INTO Positions VALUES (5, \'driver\', 30000, 4)");
        executesql("SELECT * FROM Man");
        String[] p = {"Worker", "Cleaner", "Security"};
        int[] sal = {28000, 22000, 24000};
        fillPosition(6, p, sal);
        try {
            st.close();
        } catch (SQLException ex) {
            System.out.println("Error #7: " + ex.getMessage());
        }
        * 
        */
        
        try {
            loadDriver("org.apache.derby.jdbc.ClientDriver");
            connect("jdbc:derby://localhost:1527/test,admin,admin");
            
            testPrepare test = new testPrepare(conn);
            test.fillTable("TMP", 1000);
            conn.close();
        } catch (SQLException sQLException) {
            System.out.println("Error sql: " + sQLException.getMessage());
        }
    }

    private static void printJdbcDrivers() {
        Enumeration<Driver> listDrivers = DriverManager.getDrivers();
        while (listDrivers.hasMoreElements())
            System.out.println(listDrivers.nextElement().getClass().getName());
    }
    
    private static void loadDriver(String name) {
        try {
            Class.forName(name);
        } catch (ClassNotFoundException ex) {
            System.out.println("Error #1: " + ex.getMessage());
        }
    }

    
    private static void connect(String url) {
        String str[] = url.split(","); 
        
        try {
            if (str.length < 3) conn = DriverManager.getConnection(url);
            else conn = DriverManager.getConnection(str[0].trim(), str[1].trim(), str[2].trim());
        } catch (SQLException ex) {
            System.out.println("Error #2: " + ex.getMessage());
        }
    }
    private static void connect(String url, String user, String pass) {
        try {
            conn = DriverManager.getConnection(url, user, pass);
        } catch (SQLException ex) {
            System.out.println("Error #2: " + ex.getMessage());
        }
    }

    private static void executesql(String sql) {
        try {
            st = conn.createStatement();
            boolean flag = st.execute(sql);
            
            if (flag) {
                ResultSet res = st.getResultSet();
                System.out.println(res);
            }
            else {
                int c = st.getUpdateCount();
                System.out.println("Result c=" + c);
                
            }
        } catch (SQLException ex) {
            System.out.println("Error #3 :" + ex.getMessage());
        }
        
    }
    public static void fillPosition(int begin, String[] pos, int[] sal) {
        
        if (pos.length < 1 || pos.length != sal.length) {
            System.out.println("Error #5: Invalid parameters");
            return;
        }
        
        String str = "INSERT INTO Positions VALUES (";
        try {
            st.clearBatch();
        
            for (int i = 0; i < pos.length; i++) {
                String q = str + begin  + ", \'" + pos[i] + "\', " + sal[i] + ", NULL)";
                System.out.println(q);
                st.addBatch(q);
                begin++;
            }

            st.executeBatch();
        
        } catch (SQLException ex) {
            System.out.println("Error #6: " + ex.getMessage());
        }

    }

}
